﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Common;

namespace SPC.Data
{
    public class XlsImport: IDisposable   
    {
        DataTable dt = new DataTable();
        public XlsImport() { }
        public double[][] ReadDataForXbar_R(string file)
        {
            XlsHelper xls = new XlsHelper(file);
            dt = xls.ExcelToDatatable(0, true,true);
            double[][] x = new double[dt.Columns.Count][];
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                x[i] = new double[dt.Rows.Count];
                for (int k = 0; k < dt.Rows.Count; k++)
                {
                    x[i][k] = double.Parse(dt.Rows[k][i].ToString());
                }
            }
            return x;
        }
        public double[][] ReadDataForXbar_S(string file)
        {
            return ReadDataForXbar_R(file);
        }
        public double [] ReadDataForX_Rm(string file)
        {
            XlsHelper xls = new XlsHelper(file);
            dt = xls.ExcelToDatatable(0, true, true);
            double[] x = new double[dt.Rows.Count];
            for (int i = 0; i < x.Length; i++)
            {
                double.TryParse(dt.Rows[i][0].ToString(), out x[i] );
            }
            return x;
        }
        public double[][] ReadDataForPchart(string file)
        {
            XlsHelper xls = new XlsHelper(file);
            dt = xls.ExcelToDatatable(0, true, true);
            if (dt.Columns.Count < 2)
            {
                throw (new Exception("数据列数不足2列，格式非法？"));
            }
            double[][] x = new double[dt.Rows.Count][];
            for (int i = 0; i < x.Length; i++)
            {                
                x[i] = new double[2];
                x[i][0] = int.Parse(dt.Rows[i][0].ToString());
                x[i][1]= double.Parse(dt.Rows[i][1].ToString());
            }
            return x;
        }
        public double[][]ReadDataForNPchart(string file)
        {
            return ReadDataForPchart(file);
        }
        public double[] ReadDataForCchart(string file)
        {
            XlsHelper xls = new XlsHelper(file);
            dt = xls.ExcelToDatatable(0, true, true);
            if (dt.Columns.Count < 2)
            {
                throw (new Exception("数据列数不足2列，文件格式非法？"));
            }
            double[] x = new double[dt.Rows.Count];
            for (int i = 0; i < x.Length; i++)
            {
                x[i] = double.Parse(dt.Rows[i][1].ToString());
            }
            return x;
        }
        public double[][] ReadDataForUchart(string file)
        {
            return ReadDataForPchart(file);
        }
        public DataTable GetRawData() { return dt; }
        public SSpcSpecs ReadSpcSpec(string file)
        {
            XlsHelper xls = new XlsHelper(file);
            dt = xls.ExcelToDatatable(1, true, false);
            SSpcSpecs sspec = new SSpcSpecs();
            if(dt.Columns.Count>=2)
            {
                foreach (DataRow r in dt.Rows)
                {
                    string rtitle = (string)r[0];
                    if (rtitle.ToUpper().Contains("USL") || rtitle.Contains("规格上限"))
                    {
                        double.TryParse(r[1].ToString(), out sspec.usl);
                    }
                    else if (rtitle.ToUpper().Contains("LSL") || rtitle.Contains("规格下限"))
                    {
                        double.TryParse(r[1].ToString(), out sspec.lsl);
                    }
                    else if (rtitle.ToUpper().Contains("CPKLIMIT"))
                    {
                        double.TryParse(r[1].ToString(), out sspec.cpkLimt);
                    }
                    else if (rtitle.ToUpper().Contains("UCL"))
                    {
                        double.TryParse(r[1].ToString(), out sspec.ucl);
                    }
                    else if (rtitle.ToUpper().Contains("LCL"))
                    {
                        double.TryParse(r[1].ToString(), out sspec.lcl);
                    }
                }
            }
            return sspec;            
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // dispose managed resources
                if(dt!=null)
                {                    
                    dt.Dispose();
                }
                    
            }
            // free native resources
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

    }
    public struct SSpcSpecs
    {
        public double usl;
        public double lsl;
        public double cpkLimt;
        public double ucl;
        public double lcl;
    }
}
